package org.jeecg.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

/**
 * @Description:Jackson JSON工具类
 * @Author: lizhaofei
 * @Date:
 * @Version:
 */
public class JacksonUtils {

	public static ObjectMapper objectMapper;

	/**
	 * 反序列化JSON串实体，实体没有的字段忽略
	 *
	 * @param           <T> 实体
	 * @param jsonStr   JSON字符串
	 * @param valueType T实体名称.class，例如User.class
	 *
	 * @return T实体
	 */
	public static <T> T parseObject(final String jsonStr, final Class<T> valueType) {
		if (objectMapper == null) {
			// objectMapper = new ObjectMapper();
			initObjectMapper();
		}
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return (T) objectMapper.readValue(jsonStr, (Class) valueType);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * json转换成对象列表List(T)，实体没有的字段忽略 如异常，返回null
	 *
	 * @param         <T> 实体
	 * @param jsonStr JSON字符串
	 * @param clazz   T实体名称.class，例如User.class
	 * @return List(T)
	 */
	public static <T> List<T> parseList(final String jsonStr, final Class<T> clazz) {
		if (objectMapper == null) {
			// objectMapper = new ObjectMapper();
			initObjectMapper();
		}
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);

		try {

			return (List<T>) objectMapper.readValue(jsonStr, javaType);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 转换为json字符串,不忽略NULL和空值
	 *
	 * @param object 需要转成json串的实体
	 * @return JSON字符串
	 */
	public static String toJson(final Object object) {
		if (objectMapper == null) {
			// objectMapper = new ObjectMapper();
			initObjectMapper();
		}
		try {
			if (object == null) {
				return "";
			}
			if (object instanceof String) {
				return object.toString();
			}
			return JacksonUtils.objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	/**
	 * 转换为json字符串,忽略NULL值,不忽略""值
	 *
	 * @param object 需要转成json串的实体
	 * @return JSON字符串
	 */
	public static String toJsonString(final Object object) {
		if (objectMapper == null) {
			// objectMapper = new ObjectMapper();
			initObjectMapper();
		}
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		try {
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 转换为json字符串,忽略NULL值,不忽略""值
	 *
	 * @param object 需要转成json串的实体
	 * @return JSON字符串
	 */
	public static String toJsonStringNonEmpty(final Object object) {
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
		try {
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static void initObjectMapper() {
		(objectMapper = new ObjectMapper()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
		objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		objectMapper.setDateFormat((DateFormat) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	}

	static {
		initObjectMapper();
	}
}
